package com.cityos.android.mystudynestedscroll.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.cityos.android.mystudynestedscroll.R
import com.cityos.android.mystudynestedscroll.databinding.ActivityEasyBehaviorBinding
import com.cityos.android.mystudynestedscroll.databinding.ActivityUcHomeBinding
import com.cityos.android.mystudynestedscroll.ui.fragment.ListFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.android.material.tabs.TabLayout.Tab
import com.google.android.material.tabs.TabLayoutMediator
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class UcHomeActivity : AppCompatActivity() {

    private lateinit var mainBinding: ActivityUcHomeBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainBinding = ActivityUcHomeBinding.inflate(layoutInflater).apply {
            setContentView(this.root)
        }
        initBanner()
        initViewPage()

    }

    private fun initBanner() {
        val imgIdList = ArrayList<Int>()
        for (i in 1 .. 11) {
            val imgId = resources.getIdentifier("image$i" , "drawable" , packageName)
            imgIdList.add(imgId)
        }
        val ucHomeBannerAdapter = UcHomeBannerAdapter(imgIdList)
        mainBinding.ucHomeBanner.setAdapter(ucHomeBannerAdapter)
    }

    private fun initViewPage() {
        mainBinding.ucHomeViewpage.adapter = UcContentAdapter(6 , this)
        mainBinding.ucHomeViewpage.isUserInputEnabled = false
        TabLayoutMediator(mainBinding.ucHomeTabLayout , mainBinding.ucHomeViewpage , true , true) { tab , position ->
            tab.text = "Fragment$position"
        }.attach()
    }
}


class UcHomeBannerAdapter(mData: MutableList<Int>) : BannerImageAdapter<Int>(mData) {
    override fun onBindView(holder: BannerImageHolder? , data: Int? , position: Int , size: Int) {
        holder?.imageView?.setImageResource(mDatas[position])
    }
}

class UcContentAdapter(val tabSize: Int , fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
    override fun getItemCount() = tabSize
    override fun createFragment(position: Int) = ListFragment.newInstance()
}